package com.pet.server.ext.action.pet;

import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.BeanToJson;
import com.pet.server.ext.Result;
import com.pet.server.ext.data.PetEvent;
import com.pet.server.ext.service.UserService;
import com.pet.server.ext.util.DateUtil;
import com.pet.server.ext.util.NumberUtil;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.StringUtil;

public class Eventsu extends ActionBase{
	
	public Integer id,pet_id;
	public Long event_time;
	public String type,label;
	
	
	public Eventsu(){
		 needCheckAuth =true;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {	
		id = NumberUtil.getInteger(params.getFieldValue("id"));
		pet_id = NumberUtil.getInteger(params.getFieldValue("pet_id"));
		event_time = NumberUtil.getLong(params.getFieldValue("event_time"),null);
		type = params.getFieldValue("type");
		label = params.getFieldValue("label");
		if(StringUtil.isEmpty(pet_id,type,label,event_time)){
			throw ApiError.Type.INVALID_PARAM.toException();
		}
	}
	@Override
	public Result invokeService() throws ApiError {
		UserService userService = new UserService();
		PetEvent event=null;
		if(null!=id){
			event = userService.findPetEvent(id); 
		}
		if(null==event){
			event = new PetEvent();
			event.event_time=DateUtil.getDate(event_time);
			event.pet_id=pet_id;
			event.type=type;
			event.label=label;
			userService.savePetEvent(event);
		}else{
			event.event_time=DateUtil.getDate(event_time);
			event.pet_id=pet_id;
			event.type=type;
			event.label=label;
			userService.updatePetEvent(event);
		}
		Result result = new Result();
		result.put("data", BeanToJson.objectToJson(event));
	    return result;
	}
}
